package com.example.sports_meet.athlete;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;
import com.example.sports_meet.adapter.SelectAdapter;
import com.example.sports_meet.entity.SelectM;

import java.util.ArrayList;
import java.util.List;

public class Athlete_seleteMessage extends AppCompatActivity {

    private Button btnS, btnSJ, btnTJ, btnBS;
    private ListView listView;
    private List<SelectM> books = new ArrayList<SelectM>();
    MyDbHelper myDbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_athlete_selete_message);

        myDbHelper = new MyDbHelper(Athlete_seleteMessage.this, "Sports.db", null, 1);
        btnS = (Button) findViewById(R.id.btns);
        btnSJ = (Button) findViewById(R.id.btnsj);
        btnTJ = (Button) findViewById(R.id.btntj);
        btnBS = (Button) findViewById(R.id.btnbs);
        listView = (ListView) findViewById(R.id.listview);

        btnS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                books.clear();
                SelectM selectMs = new SelectM("项目号", "项目名", "比赛时间");
                books.add(selectMs);
                //db=managerAthleteHelper.getReadableDatabase();
                db = myDbHelper.getReadableDatabase();
                Cursor cursor = db.query("eevent", new String[]{"eno", "ename", "etime"}, "fno=?", new String[]{"202207001"}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(Athlete_seleteMessage.this, "没有数据", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    for (int i = 0; i < cursor.getCount(); i++) {
                        String eno = cursor.getString(0);
                        String ename = cursor.getString(1);
                        String etime = cursor.getString(2);
                        cursor.moveToNext();
                        SelectM selectM = new SelectM(eno, ename, etime);
                        books.add(selectM);
                    }
                }
                cursor.close();
                db.close();
                SelectAdapter selectAdapter = new SelectAdapter(books, Athlete_seleteMessage.this);
                listView.setAdapter(selectAdapter);
            }
        });
        btnSJ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                books.clear();
                SelectM selectMs = new SelectM("项目号", "项目名", "比赛时间");
                books.add(selectMs);

                //db=managerAthleteHelper.getReadableDatabase();
                db = myDbHelper.getReadableDatabase();
                Cursor cursor = db.query("eevent", new String[]{"eno", "ename", "etime"}, "fno=?", new String[]{"202207002"}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(Athlete_seleteMessage.this, "没有数据", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    for (int i = 0; i < cursor.getCount(); i++) {
                        String eno = cursor.getString(0);
                        String ename = cursor.getString(1);
                        String etime = cursor.getString(2);
                        cursor.moveToNext();
                        SelectM selectM = new SelectM(eno, ename, etime);
                        books.add(selectM);
                    }
                }
                cursor.close();
                db.close();
                SelectAdapter selectAdapter = new SelectAdapter(books, Athlete_seleteMessage.this);
                listView.setAdapter(selectAdapter);
            }
        });
        btnTJ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                books.clear();
                SelectM selectMs = new SelectM("项目号", "项目名", "比赛时间");
                books.add(selectMs);
                //db=managerAthleteHelper.getReadableDatabase();
                db = myDbHelper.getReadableDatabase();
                Cursor cursor = db.query("eevent", new String[]{"eno", "ename", "etime"}, "fno=?", new String[]{"202207003"}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(Athlete_seleteMessage.this, "没有数据", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    for (int i = 0; i < cursor.getCount(); i++) {
                        String eno = cursor.getString(0);
                        String ename = cursor.getString(1);
                        String etime = cursor.getString(2);
                        cursor.moveToNext();
                        SelectM selectM = new SelectM(eno, ename, etime);
                        books.add(selectM);
                    }
                }
                cursor.close();
                db.close();
                SelectAdapter selectAdapter = new SelectAdapter(books, Athlete_seleteMessage.this);
                listView.setAdapter(selectAdapter);
            }
        });
        btnBS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                books.clear();
                SelectM selectMs = new SelectM("项目号", "项目名", "比赛时间");
                books.add(selectMs);
                //db=managerAthleteHelper.getReadableDatabase();
                db = myDbHelper.getReadableDatabase();
                Cursor cursor = db.query("eevent", new String[]{"eno", "ename", "etime"}, "fno=?", new String[]{"202207004"}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(Athlete_seleteMessage.this, "没有数据", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    for (int i = 0; i < cursor.getCount(); i++) {
                        String eno = cursor.getString(0);
                        String ename = cursor.getString(1);
                        String etime = cursor.getString(2);
                        cursor.moveToNext();
                        SelectM selectM = new SelectM(eno, ename, etime);
                        books.add(selectM);
                    }
                }
                cursor.close();
                db.close();
                SelectAdapter selectAdapter = new SelectAdapter(books, Athlete_seleteMessage.this);
                listView.setAdapter(selectAdapter);
            }
        });
    }
}